package cn.hy.face.module.sync.controller;

import cn.hy.face.common.enums.ResultCode;
import cn.hy.face.common.service.FaceRecognitionService;
import cn.hy.face.common.vo.Result;
import cn.hy.face.common.vo.SysStudentVO;
import cn.hy.face.module.sync.dto.FaceInfoDTO;
import cn.hy.face.module.sync.dto.FaceInfoRequestDTO;
import cn.hy.face.module.sync.dto.SysResultDTO;
import cn.hy.face.module.sync.dto.SysStudentDTO;
import org.apache.commons.lang3.StringUtils;
import org.apache.thrift.TException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
/**
 * @program: face
 * @author: qingxiangen
 * @create: 2019-09-05 10:22
 * @description: 同步数据Controller - 安卓端
 **/
@RestController
@RequestMapping("/sync")
public class SyncController {
    
    /**
     * 人脸识别服务
     */
    @Autowired
    private FaceRecognitionService faceRecognitionService;
    
    /**
     * 更新人脸照片校验状态
     * @param schoolId 学校id
     * @param userId 学生id
     * @param status 1:成功，其他:失败
     * @param message 失败原因
     * @return 
     * @throws TException
     */
    @RequestMapping("/syncFaceStatus")
    public Result syncFaceStatus(@RequestParam String schoolId, @RequestParam String userId, @RequestParam Byte status, @RequestParam Long updateAt, String message) throws TException {
        //校验参数
        validateParams(schoolId);
        
        faceRecognitionService.updateFaceStatusToAndroid(schoolId, userId, status, message, updateAt);
        return Result.success(null);
    }

    /**
     * 更新人脸照片校验状态 - 批量更新
     * @param requestDTO 人脸状态信息
     * @return
     * @throws TException
     */
    @RequestMapping("/syncFaceStatusList")
    public Result syncFaceStatusList(@RequestBody FaceInfoRequestDTO requestDTO) throws TException {
       
        if(requestDTO == null){
             new TException(ResultCode.PARAM_ERROR.getMessage());
        }
        
        //校验参数
        validateParams(requestDTO.getSchoolId());
        
        if(CollectionUtils.isEmpty(requestDTO.getFaceStatusList())){
            return Result.success(null);
        }
        for (FaceInfoDTO faceInfoDTO : requestDTO.getFaceStatusList()) {
            faceRecognitionService.updateFaceStatusToAndroid(requestDTO.getSchoolId(), faceInfoDTO.getUserId(), faceInfoDTO.getStatus(), faceInfoDTO.getMessage(), faceInfoDTO.getUpdateAt());
        }
        return Result.success(null);
    }

    /**
     * 根据班级id获取所有学生信息
     * @param schoolId 学校id
     * @param classId 班级id
     * @param preSyncTime 增量查询时间，null: 全量查询
     * @return
     * @throws TException
     */
    @RequestMapping("/download/students")
    public Result getUserInfoList(@RequestParam String schoolId, @RequestParam String classId, Long preSyncTime) throws TException {
        //校验参数
        validateParams(schoolId);
        
        long curMillis = System.currentTimeMillis();
        SysResultDTO<List<SysStudentDTO>> sysResult = new SysResultDTO<List<SysStudentDTO>>();
        List<SysStudentVO> stuList = faceRecognitionService.getUsersByClassIdToAndroid(schoolId, classId, preSyncTime);
        if(CollectionUtils.isEmpty(stuList)){
            sysResult.setSystemTime(curMillis);
            sysResult.setData(new ArrayList<>());
            return Result.success(sysResult);
        }
        List<SysStudentDTO> stuDTOList = new ArrayList<>();
        for (SysStudentVO stuVO: stuList) {
            if(stuVO.getId() == null){
                continue;
            }
            stuDTOList.add(buildStuDTO(stuVO));
        }
        sysResult.setSystemTime(curMillis);
        sysResult.setData(stuDTOList);
        return Result.success(sysResult);
    }

    /**
     * 根据学生id获取学生人脸信息
     * @param schoolId 学校id
     * @param userId 学生id
     * @return
     * @throws TException
     */
    @RequestMapping("/download/student")
    public Result getUserInfoList(@RequestParam String schoolId, @RequestParam String userId) throws TException {
        //校验参数
        validateParams(schoolId);
        
        long curMillis = System.currentTimeMillis();
        SysResultDTO<SysStudentDTO> sysResult = new SysResultDTO<SysStudentDTO>();
        SysStudentVO stuVO = faceRecognitionService.findFaceInfoByUserIdToAndroid(schoolId, userId);
        sysResult.setSystemTime(curMillis);
        sysResult.setData(buildStuDTO(stuVO));
        return Result.success(sysResult);
    }

    /**
     * 构建人脸信息传输对象
     * @param stuVO
     * @return
     */
    private SysStudentDTO buildStuDTO(SysStudentVO stuVO){
        if(stuVO == null){
            return null;
        }
        SysStudentDTO stuDTO = new SysStudentDTO();
        stuDTO.setId(stuVO.getId());
        stuDTO.setUid(stuVO.getUserId());
        stuDTO.setName(stuVO.getRealName());
        stuDTO.setAvatar(stuVO.getFacePhoto());
        stuDTO.setStatus(stuVO.getStatus());
        stuDTO.setCreateAt(stuVO.getCreateAt() == null ? null: stuVO.getCreateAt().getTime());
        stuDTO.setUpdateAt(stuVO.getUpdateAt() == null ? null: stuVO.getUpdateAt().getTime());
        return stuDTO;
    }

    /**
     * 校验学校id
     * @param schoolId
     * @throws TException
     */
    private void validateParams(String schoolId) throws  TException{
        if(StringUtils.isBlank(schoolId)){
           new TException(ResultCode.PARAM_ERROR.getMessage());
        }
    }
}
